<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span='8'>
          <!-- 搜索区域 -->
          <el-input v-model='queryInfo.query' placeholder="请输入内容">
            <el-button slot="append" @click='getOrderList' icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格列表区域 -->
      <el-table :data="orderList" style="width: 100%">
        <el-table-column type='index' label='#'></el-table-column>
        <el-table-column prop="order_number" width='200px' label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="order_pay" label="是否付款">
          <template slot-scope='scope'>
            <el-tag v-if='scope.row.order_pay === "1"' type='success'>已付款</el-tag>
            <el-tag v-else type='danger'>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货">
          <template slot-scope='scope'>
            {{ scope.row.is_send}}
          </template>`
        </el-table-column>
        <el-table-column prop="create_time" width='160px' label="下单时间">
          <template slot-scope='scope'>
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button @click='showCitysDialog' size='mini' type="primary" icon="el-icon-edit"></el-button>
            <el-button @click='showProgressBox' size='mini' type="success" icon="el-icon-location"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="10"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 省市县对话框 -->
    <el-dialog @close='cityDialogClosed' title="修改地址" :visible.sync="cityDialogVisable" width="50%">
      <!-- 表单 -->
      <el-form :model="editAddressForm" :rules="editAddressFormRules" ref="editAddressFormRef" label-width="100px"
        class="demo-ruleForm">
        <el-form-item label="省市区/县" prop="address1">
          <!-- <el-input v-model="editAddressForm.address1"></!-->
          <el-cascader v-model="editAddressForm.address1" :options="cityData" :props="{ expandTrigger: 'hover' }">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editAddressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cityDialogVisable = false">取 消</el-button>
        <el-button type="primary" @click="cityDialogVisable = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressDialogVisiable" width="50%">
      <!-- 时间线组件 -->
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in progressData" :key="index" :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
  // 引入省市区/县数据
  import cityData from './citydata.js'
  export default {
    data() {
      return {
        // 查询条件信息
        queryInfo: {
          query: '',
          pagesize: 10,
          pagenum: 1
        },
        // 保存订单数据信息
        orderList: [],
        // 总订单数量
        total: 0,
        // 省市县三级联动对话框显示与隐藏
        cityDialogVisable: false,
        // 修改地址的表单数据
        editAddressForm: {
          address1: [],
          address2: ''
        },
        // 修改地址表单验证规则
        editAddressFormRules: {
          address1: [{
            required: true,
            message: '请选择省市区/县',
            trigger: 'blur'
          }],
          address2: [{
            required: true,
            message: '请输入详细地址信息',
            trigger: 'blur'
          }]
        },
        // 保存引入的城市数据信息
        cityData,
        // 物流进度对话框的隐藏与显示
        progressDialogVisiable: false,
        // 保存物流信息
        progressData: []
      }
    },
    created() {
      this.getOrderList()
    },
    methods: {
      // 获取订单列表信息
      async getOrderList() {
        if (!this.queryInfo.query.trim()) {
          this.queryInfo.query = ''
        }
        this.queryInfo.query = this.queryInfo.query.trim()
        const {
          data: res
        } = await this.$http.get('orders', {
          params: this.queryInfo
        })
        if (res.meta.status !== 200) return this.$message.error('获取订单数据失败')

        // 获取订单成功
        // this.$message.success('获取订单数据成功')
        // console.log(res.data)
        this.orderList = res.data.goods
        this.total = res.data.total
      },
      // 监听页面展示条数变化的事件
      handleSizeChange(newSize) {
        console.log(newSize)
        this.queryInfo.pagesize = newSize
        this.getOrderList()
      },
      // 监听页码变化的事件
      handleCurrentChange(newPage) {
        console.log(newPage)
        this.queryInfo.pagenum = newPage
        this.getOrderList()
      },
      // 展示省市县三级联动对话框
      showCitysDialog() {
        this.cityDialogVisable = true
      },
      // 城市对话框关闭触发的事件
      cityDialogClosed() {
        // 重置表单
        this.$refs.editAddressFormRef.resetFields()
        this.editAddressForm.address1 = []
      },
      // 打开物流进度的对话框
      async showProgressBox() {
        // 获取物流信息
        const {
          data: res
        } = await this.$http.get('/kuaidi/1106975712662')
        if (res.meta.status !== 200) return this.$message.error('获取物流信息失败')

        // 获取物流数据成功
        this.$message.success('获取物流信息成功')
        // console.log(res.data)
        this.progressData = res.data
        // 显示物流进度对话框
        this.progressDialogVisiable = true
      }
    }
  }

</script>

<style lang="less" scoped>
  /* 引入timeline的样式文件 */
  @import '../../plugins/timeline/timeline.css';
  @import '../../plugins/timeline-item/timeline-item.css';

  .el-cascader {
    width: 100%;
  }

</style>
